package com.yzx.lifeassistants.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebSettings.TextSize;
import android.webkit.WebView;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yzx.lifeassistants.R;
import com.yzx.lifeassistants.base.BaseActivity;
import com.yzx.lifeassistants.bean.ActivityInfo;
import com.yzx.lifeassistants.model.IQueryActivityInfo;
import com.yzx.lifeassistants.model.callback.IQueryActivityInfoCB;
import com.yzx.lifeassistants.model.impl.QueryActivityInfoImpl;
import com.yzx.lifeassistants.utils.ToastUtils;
import com.yzx.lifeassistants.view.widget.CircularLoadingDialog;

/**
 * @Description: 活动详情界面
 * @author: yzx
 * @time: 2016-3-29 下午2:50:59
 */
@SuppressWarnings("deprecation")
public class ActivityInfoActivity extends BaseActivity implements
		OnClickListener {
	private ImageButton backBtn;// 返回按钮
	private LinearLayout titleLL;// 标题栏
	private TextView typeTV;// 类型
	private TextView titleTV;// 标题
	private WebView webView;// 加载网页

	private Handler handler;//
	private IQueryActivityInfoCB queryCallBack;// 回调
	private IQueryActivityInfo queryActivityInfo;// 查询
	private CircularLoadingDialog dialog;// 加载对话框
	private String url;// URL
	private String title;// 标题
	/**
	 * @Description: Html页面头部
	 */
	public static final String ACTIVITY_INFO_HTML_HEAD = "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>"
			+ "<html xmlns='http://www.w3.org/1999/xhtml'>"
			+ "<head>"
			+ "<meta name='viewport' content='width=device-width, initial-scale=1.0, minimum-scale=0.8, maximum-scale=1.0, user-scalable=yes'/>"
			+ "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />"
			+ "</head>" + "<body>";

	/**
	 * @Description: Html页面尾部
	 */
	public static final String ACTIVITY_INFO_HTML_END = "</body>"
			+ "<script>  document.body.style.lineHeight = 1.5 </script>"
			+ "</html>";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_activity_info);
		init();
		initView();
		setListener();
		initData();
	}

	/**
	 * 
	 * @Description: 初始化
	 */
	private void init() {
		Intent intent = getIntent();
		if (null == intent) {
			url = "";
			title = "";
		} else {
			url = intent.getStringExtra("ACTIVITY_INFO_URL");
			title = intent.getStringExtra("ACTIVITY_INFO_TITLE");
		}
	}

	/**
	 * 
	 * @Description: 初始化控件
	 */
	private void initView() {
		backBtn = (ImageButton) findViewById(R.id.activity_info_back_btn);
		titleLL = (LinearLayout) findViewById(R.id.activity_info_title_ll);
		typeTV = (TextView) findViewById(R.id.activity_info_type_tv);
		titleTV = (TextView) findViewById(R.id.activity_info_title_tv);
		webView = (WebView) findViewById(R.id.activity_info_wv);
	}

	/**
	 * 
	 * @Description: 设置监听
	 */
	private void setListener() {
		backBtn.setOnClickListener(this);
	}

	/**
	 * 
	 * @Description: 初始化数据
	 */
	@SuppressLint("SetJavaScriptEnabled")
	private void initData() {
		if (title.contains("【")) {
			typeTV.setVisibility(View.VISIBLE);
			typeTV.setText(title.substring(1, title.indexOf("】")));
			titleTV.setText(title.substring(title.indexOf("】") + 1,
					title.length()));
		} else {
			typeTV.setVisibility(View.GONE);
			titleTV.setText(title);
		}

		dialog = new CircularLoadingDialog(this);
		WebSettings webSettings = webView.getSettings();
		webView.getSettings().setSupportZoom(true);// 支持屏幕缩放
		webView.getSettings().setBuiltInZoomControls(true);
		webView.getSettings().setDisplayZoomControls(false);// 不显示webview缩放按钮
		webSettings.setJavaScriptEnabled(true);
		webSettings.setAllowContentAccess(true);
		webSettings.setAppCacheEnabled(false);
		webSettings.setBuiltInZoomControls(true);
		webSettings.setUseWideViewPort(true);
		webSettings.setLoadWithOverviewMode(true);
		webSettings.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
		webView.setWebChromeClient(new WebChromeClient() {

			@Override
			public void onProgressChanged(WebView view, int newProgress) {

				if (newProgress == 100) {
					dialog.dismiss();
				}
				super.onProgressChanged(view, newProgress);
			}

		});
		handler = new Handler();
		queryCallBack = new IQueryActivityInfoCB() {

			@Override
			public void querySuccess(final ActivityInfo activityInfo) {
				handler.post(new Runnable() {

					@Override
					public void run() {
						String content = activityInfo.getContent();
						String title = activityInfo.getTitle();
						if (title.contains("【")) {
							typeTV.setVisibility(View.VISIBLE);
							typeTV.setText(title.substring(1,
									title.indexOf("】")));
							titleTV.setText(title.substring(
									title.indexOf("】") + 1, title.length()));
						} else {
							typeTV.setVisibility(View.GONE);
							titleTV.setText(title);
						}
						// 如果内容里已经包含了标题则隐藏
						if (content.contains(title.substring(
								title.indexOf("】") + 1, title.length()))) {
							titleLL.setVisibility(View.VISIBLE);
						} else {
							titleLL.setVisibility(View.VISIBLE);
						}
						if (content.contains("src")) {
							webView.getSettings().setTextSize(TextSize.SMALLER);
						}
						String html = ACTIVITY_INFO_HTML_HEAD
								+ activityInfo.getContent()
								+ ACTIVITY_INFO_HTML_END;
						webView.loadDataWithBaseURL(null, html, "text/html",
								"utf-8", null);
						dialog.dismiss();
					}
				});
			}

			@Override
			public void queryError(final int code) {
				handler.post(new Runnable() {

					@Override
					public void run() {
						dialog.dismiss();
						switch (code) {
						case IQueryActivityInfoCB.RESULR_NULL: {
							showToast("获取失败，请确认网络连接后刷新重试~");
							break;
						}
						case IQueryActivityInfoCB.QUERY_ERROR: {
							showToast("获取异常，请确认网络连接后刷新重试~");
							break;
						}
						default:
							break;
						}
					}
				});
			}
		};
		queryActivityInfo = new QueryActivityInfoImpl(this, queryCallBack);
		queryActivityInfo();
	}

	/**
	 * 
	 * @Description: 获取活动详情
	 */
	private void queryActivityInfo() {
		if (null == url || "".equals(url)) {
			showToast("获取异常，请确认网络连接后刷新重试~");
		} else {
			dialog.show();
			queryActivityInfo.queryActivityInfo(url);
		}
	}

	/**
	 * 
	 * @Description: 按键监听
	 */
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.activity_info_back_btn: {
			finish();
			break;
		}

		default:
			break;
		}
	}

	/**
	 * 
	 * @Description: Toast显示信息
	 */
	private void showToast(String message) {
		ToastUtils.showAlertToast(this, message, R.id.activity_info_top_ll);

	}
}
